import 'package:flutter/material.dart';

class CounterFieldExample extends FormField<int> {
  CounterFieldExample(
      {FormFieldSetter<int> onSaved,
      FormFieldValidator<int> validator,
      int initialValue = 0,
      bool autovalidate: false,
      bool enabled: true})
      : super(
            onSaved: onSaved,
            validator: validator,
            initialValue: initialValue,
            autovalidate: autovalidate,
            builder: (FormFieldState<int> field) {
              return Column(
                children: [
                  Row(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      IconButton(
                        icon: Icon(Icons.remove),
                        onPressed: () {
                          field.didChange(field.value - 1);
                          print("remove ${field.value}");
                        },
                      ),
                      Padding(
                        padding: const EdgeInsets.only(left: 20.0, right: 20.0),
                        child: Text(field.value.toString()),
                      ),
                      IconButton(
                        icon: Icon(Icons.add),
                        onPressed: () {
                          field.didChange(field.value + 1);
                          print("add ${field.value}");
                        },
                      )
                    ],
                  ),
                  // 添加错误提示
                  field.hasError
                      ? Text(
                          field.errorText,
                          style: TextStyle(color: Colors.red),
                        )
                      : Container()
                ],
              );
            });
}
